package com.nativetalk.service.three.course;

import com.nativetalk.base.RetInfo;
import com.nativetalk.bean.course.TdNtCoursePictureList;
import com.nativetalk.bean.course.TsNtCourseInfoIndexTwo;
import com.nativetalk.bean.course.TwoTsNtCourseInfoDetail;
import com.nativetalk.bean.setmeal.TsNtSetmealIndex;
import com.nativetalk.common.ValidationMessages;
import com.nativetalk.common.util.MemcachedKey;
import com.nativetalk.common.util.MemcachedUtils;
import com.nativetalk.dao.TdNtBannerInfoDao;
import com.nativetalk.dao.TsNtCourseInfoDao;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * Created by lizhun on 2016/9/26.
 */
@Service
public class CourseServiceImpl implements CourseService {
    private static Logger logger = Logger.getLogger(CourseServiceImpl.class);
    @Resource
    private TdNtBannerInfoDao tdNtBannerInfoDao;

    @Resource
    private TsNtCourseInfoDao courseInfoDao;

    @Override
    public RetInfo index() {
        String logInfo = this.getClass().getName() + ":index:";
        // 类路径+方法名
        logger.info("======" + logInfo + "begin======");
        RetInfo retInfo = new RetInfo();
        try {

            Map<String, Object> map = new HashMap<>();
            /**
             * 查询banner图
             */
            map.put("banner", tdNtBannerInfoDao.findTdNtBannerInfoIndexByType(2));


            /**
             * 查询课程信息
             */
            map.put("course", MemcachedUtils.get(MemcachedKey.tsNtCategoryIndices));
            /**
             * 查询视频介绍图
             */
            map.put("video", tdNtBannerInfoDao.findTdNtBannerInfoIndexByType(3));

            retInfo.setMark(ValidationMessages.getValue("MARKYES"));
            retInfo.setTip("获取数据成功");
            retInfo.setObj(map);
        } catch (Exception e) {
            retInfo.setMark(ValidationMessages.getValue("MARKERROR"));
            retInfo.setTip(ValidationMessages.getValue("TIPERROR"));
            logger.error(e.getMessage(), e);
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }

    @Override
    public RetInfo list(String cate_id) {
        String logInfo = this.getClass().getName() + ":list:";
        // 类路径+方法名
        logger.info("======" + logInfo + "begin======");
        RetInfo retInfo = new RetInfo();
        try {
            // Map <key:cou_id, value:setmealList>
            Map<String, List<TsNtSetmealIndex>> tsNtSetmealSynopisMap = (Map<String, List<TsNtSetmealIndex>>) MemcachedUtils.get(MemcachedKey.tsNtSetmealIndexMap);

            Map<String, Object> map = new HashMap<>();

            // Map <key:cate_id, value:courseInfoList>
            Map<String, List<TsNtCourseInfoIndexTwo>> courseMapIdTwo = (Map<String, List<TsNtCourseInfoIndexTwo>>) MemcachedUtils.get(MemcachedKey.courseMapIdTwo);
            List<TsNtCourseInfoIndexTwo> tsNtCourseInfoIndexTwos = courseMapIdTwo.get(cate_id);//课程二级分类列表

            // Map <key:cou_id, value:courseInfoList>
            Map<String, TwoTsNtCourseInfoDetail> tsNtCourseInfoDetailMapTwo = (Map<String, TwoTsNtCourseInfoDetail>) MemcachedUtils.get(MemcachedKey.tsNtCourseInfoDetailMapTwo);

            Iterator<TsNtCourseInfoIndexTwo> iter = tsNtCourseInfoIndexTwos.iterator();
            while (iter.hasNext()) {
                TsNtCourseInfoIndexTwo courseInfo = iter.next();
                List<TsNtSetmealIndex> tsNtSetmealIndexs = tsNtSetmealSynopisMap.get(courseInfo.getCou_id().toString());

                /**
                 * 设置课程展示价格为对应的规格套餐里最低的
                 */
                int tmp = 0;
                for (int i = 0; i < tsNtSetmealIndexs.size(); i++) {
                    if (0 == i) {
                        continue;
                    }
                    if (0 > tsNtSetmealIndexs.get(tmp).getSet_amout().compareTo(tsNtSetmealIndexs.get(i).getSet_amout())) {
                        tmp = i;
                    }
                }
                courseInfo.setCou_amount(tsNtSetmealIndexs.get(tmp).getSet_amout());
                courseInfo.setSet_id(tsNtSetmealIndexs.get(tmp).getSet_id());
            }

            map.put("course", tsNtCourseInfoIndexTwos);

            retInfo.setMark(ValidationMessages.getValue("MARKYES"));
            retInfo.setTip("获取数据成功");
            retInfo.setObj(map);
        } catch (Exception e) {
            retInfo.setMark(ValidationMessages.getValue("MARKERROR"));
            retInfo.setTip(ValidationMessages.getValue("TIPERROR"));
            logger.error("==========error line:" + e.getStackTrace()[0].getLineNumber());
            logger.error(e.getMessage(), e);
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }

    public List<TdNtCoursePictureList> findCoursePicture(String str_cou_id) {
        Integer cou_id = Integer.parseInt(str_cou_id);

        List<TdNtCoursePictureList> list = courseInfoDao.findCoursePicture(cou_id);
        return list;

    }

}
